package com.letv.core.api;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.text.TextUtils;

import com.letv.core.bean.LetvBaseBean;
import com.letv.core.network.volley.Volley;
import com.letv.core.network.volley.VolleyRequest;
import com.letv.core.network.volley.VolleyResponse;
import com.letv.core.network.volley.VolleyResponse.ResponseSupplier;
import com.letv.core.network.volley.VolleyResult;
import com.letv.core.network.volley.exception.DataIsErrException;
import com.letv.core.network.volley.exception.DataIsNullException;
import com.letv.core.network.volley.exception.DataNoUpdateException;
import com.letv.core.network.volley.exception.JsonCanNotParseException;
import com.letv.core.network.volley.exception.ParseException;
import com.letv.core.network.volley.exception.TokenLoseException;
import com.letv.core.parser.LetvMasterParser;
import com.letv.core.parser.LetvMobileParser;
import com.letv.core.parser.LetvNormalParser;

/**
 * app请求服务器接口基类
 * 
 * @author zhuqiao
 * 
 */
public class LetvRequest<T extends LetvBaseBean> extends VolleyRequest<T> {
    /**
     * json解析对应的bean的class
     */
    private Class<T> mClass;
    private int mFrom;

    public LetvRequest(Context context) {
        this(context, null);
    }

    public LetvRequest(Context context, Class<T> cls) {
        this(context, cls, 0);
    }

    public LetvRequest(Context context, Class<T> cls, int from) {
        super(context);
        mClass = cls;
        mFrom = from;
    }

    @Override
    public final VolleyRequest<T> add() {
        Volley.getQueue().add(this);
        return this;
    }

    @Override
    public VolleyResult<T> syncFetch() {
        return Volley.getQueue().syncFetch(this);
    }

    @Override
    protected void parse(VolleyResponse response, ResponseSupplier supplier) throws JsonCanNotParseException,
            ParseException, DataIsNullException, DataIsErrException, DataNoUpdateException, TokenLoseException {
        if (response == null || TextUtils.isEmpty(response.data)) {
            throw new DataIsNullException();
        }

        String data = response.data;

        try {
            if (mParser == null) {
                JSONObject object = new JSONObject(data);
                if (object.has(LetvMobileParser.BODY)) {
                    setParser(new LetvMobileParser<T>(mFrom));
                } else if (object.has(LetvMasterParser.BEAN)) {
                    setParser(new LetvMasterParser<T>(mFrom));
                } else {
                    setParser(new LetvNormalParser<T>(mFrom));
                }
            }
        } catch (JSONException e) {
            throw new DataIsErrException();
        }

        if (mParser != null) {
            mParser.setClz(mClass);
            if (supplier == ResponseSupplier.NETWORK) {
                mNetworkEntry = mParser.doParse(data);
            } else {
                mCacheEntry = mParser.doParse(data);
            }
        }
    }

}
